
        var csListStore = new Ext.data.JsonStore({
			url: '/weihe/getList_shoppingList',
			root: 'datas',
			fields : [{
			        	   name:'csListId',mapping:'id'
			          },{
			        	   name:'csListCode',mapping:'code' 
			          },{
			        	   name:'csListNum',mapping:'listNum' 
			          },{
        				   name:'csState',mapping:'state'
        			  },{
        				   name:'csDate', mapping:'date',type : 'date', dateFormat:"Y-m-d"
        			  },{
        				   name:'csType',mapping:'type'
        			  },{
        				   name:'csTotal',mapping:'total'
        			  },{
        				   name:'csPayment',mapping:'payment'
        			  },{
        				   name:'csClerk',mapping:'clerk'
        			  },{
        				   name:'csRemark',mapping:'remark'
        			  }],
        	baseParams : {'id': '0'},
			listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_shoppingList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}
		});
        
        var csStore = new Ext.data.JsonStore({
			url: '/weihe/getSLCargo_shoppingList',
			root: 'datas',
			fields : [{
			        	   name:'csId',mapping:'id'
			          },{
			        	   name:'csAmount',mapping:'amount' 
			          },{
        				   name:'csPrice',mapping:'price'
        			  },{
        				   name:'csRemark',mapping:'remark'
        			  },{
        				   name:'csSum',mapping:'sum'
        			  },{
        				   name:'csOrigin',mapping:'cargo', convert:function(v){ 
														return v ? v.origin : undefined;}
        			  },{
						   name:'csCode',mapping:'cargo', convert:function(v){ 
														return v ? v.code : undefined;}
			          },{
			        	   name:'csName',mapping:'cargo', convert:function(v){ 
														return v ? v.name : undefined;}
			          },{
			        	   name:'csSpecs',mapping:'cargo', convert:function(v){ 
			        	  								return v ? v.specs : undefined;}
			          },{
			        	   name:'csUnit',mapping:'cargo', convert:function(v){ 
			        	  								return v ? v.unit : undefined;}
			          }],
			listeners :{ 
					   'beforeload':function(s,options) {
							var tag = 0;
							Ext.Ajax.request({
								url : '/weihe/getListJudge_shoppingList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									if (json.deny) {
										Ext.Msg.alert('错误',json.msg);
										tag = 1;
									}
										if(tag == 1){
											return false;
										}else{
											return true;						
										}
								},
								failure : function() {
									Ext.Msg.alert('错误', '失败!');
									tag =1;
								}
							});
			   }
			}
		});
       
        
cs = function(){
	
	var tmp='';
	
  return{
	  
	  
		listPanel : function(){
	  
		var listPanel = new Ext.Panel( {
			                    region : 'north',
			                    layout : 'border',
			                    frame  : true,
			                    height :  250,
			                    items  : [cs.listGrid(),cs.listForm()] 
		                      });
	  
			return listPanel;
  	},

  listForm : function(){
	  
	var button_add = new Ext.Button({
		text : '添加',
		handler : function() {
			progressConsole();
			if(listForm.form.isValid()){ 
			
				if (listForm.form.findField("csListId").getValue() == "") {
					 listForm.form.submit({
					     url : '/weihe/add_shoppingList',
					     success : function(form, action) {
						      if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								 }else{
							        Ext.Msg.alert('消息', action.result.msg, function() {
							        var t =listForm.form.findField("shoppingList.code").getValue();
							        listForm.form.reset();
							         Ext.getCmp("csGrid").getStore().removeAll();
							        listForm.form.findField("shoppingList.code").setValue(t);
							        Ext.getCmp("csForm").form.findField('shoppingList.id').setValue(action.result.shoppingList_id);
								    Ext.getCmp("csListGrid").getStore().load({params : {start : action.result.total-1, limit : 6 ,'id':action.result.id}});;
							        })
						       }
					     },
						 failure : function(form, action) {
      							Ext.Msg.alert('错误', action.result.msg);
      						}
				      })
				}else {
						listForm.form.submit({
							url : '/weihe/update_shoppingList',
							success : function(form, action) {
								if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								 }else{
									Ext.Msg.alert('消息', action.result.msg, function() {
										Ext.getCmp("csListGrid").getStore().reload();
									})
								}
						   },
							 failure : function(form, action) {
      							Ext.Msg.alert('错误', action.result.msg);
      						}
						})
				}
			}else{
				Ext.Msg.alert('提示','请输入相关内容');
			}
		}
	});

				var button_clear = new Ext.Button({
											text : '清空',
											handler : function() {
												var t =listForm.form.findField("shoppingList.code").getValue();
												listForm.form.reset();
												listForm.form.findField("shoppingList.code").setValue(t);
												listForm.buttons[0].setText('添加');
												Ext.getCmp("csGrid").getStore().removeAll();
											}
				});

				var button_del = new Ext.Button({
										text : '删除',
										handler : function() {
												var id = listForm.form.findField('csListId').getValue();
												if (id == '') {
													Ext.Msg.alert('提示', '请选择需要删除的信息!');
												} else {
													progressConsole();
													Ext.Ajax.request({
															url : '/weihe/delete_shoppingList',
															success : function(response) {
															var json = Ext.decode(response.responseText);
															if (json.success) {
																if (json.deny) {
																		Ext.Msg.alert('错误',json.msg);
																	var t =listForm.form.findField("shoppingList.code").getValue();
							        								listForm.form.reset();
							        								listForm.form.findField("shoppingList.code").setValue(t);
																	listForm.buttons[0].setText('添加');
															  }else{
																Ext.Msg.alert('消息', json.msg, function() {
																	Ext.getCmp("csListGrid").getStore().load({params : {start : 0, limit : 6 ,id: json.id}});
																	var t =listForm.form.findField("shoppingList.code").getValue();
							        								listForm.form.reset();
							        								listForm.form.findField("shoppingList.code").setValue(t);
																	listForm.buttons[0].setText('添加');
																});
															  }
															}else{
																Ext.Msg.alert('消息', json.msg);
															}
													},
													failure : function() {
														Ext.Msg.alert('错误', '删除失败!');
													},
													params: { 'shoppingList.id' : listForm.form.findField('shoppingList.id').getValue() }
													});
												}
								}
					});

	var paymentComboData = new Ext.data.SimpleStore({
		  fields : ['text'],
		  data : [['先款后货'],['先货后款'],['先订金 天后收余款']]
	  });
  


		var listForm = new Ext.form.FormPanel({
		id : 'csListForm',
		title : '采购单信息',
		region : 'east',
		frame : true,
		width : 300,
		autoScroll : true,
		labelAlign : 'right',
		labelWidth : 60,
		defaultType : 'textfield',
			defaults : {
			width : 200
		},
		items : [{  // Ext.form.Hidden
			id: 'csListId',
			xtype : 'hidden',  // 隐藏的表单
			name : 'shoppingList.id'
		},{  // Ext.form.Hidden
			id: 'csListCode',
			xtype : 'hidden',  // 隐藏的表单
			value : '0',
			name : 'shoppingList.code'
		},{
			id: 'csDate',
            xtype: 'datefield',
            fieldLabel: "日期", //第三行，出生日期 
            format : 'Y-m-d',
			allowBlank:false,
            name: "shoppingList.date"
		},{
			id: 'csType',
			fieldLabel: "类型",
      	    name: "shoppingList.type",
      	    value: '自购',
      	    readOnly : true
		},{
				id:'csPayment',
				fieldLabel : '支付方式',
 				name : 'shoppingList.payment',
 				xtype : 'combo',
 				store : paymentComboData,
 				emptyText : '请选择',
 				mode : 'local',
 				triggerAction : 'all',
 				allowBlank:false,
 				displayField : 'text'
		  },{
			id: 'csClerk',
			fieldLabel: "业务员",
      	    name: "shoppingList.clerk"
		}],
		buttons : [button_add,button_clear,button_del]
});
  
  return listForm;
},  
	  
listGrid : function(){
	  
	var pageToolBar = new Ext.PagingToolbar({
		pageSize : 6,
		store : csListStore,
		displayInfo : true,
		displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
	    emptyMsg : "没有记录显示!",
	    firstText : '首页',
	    prevText : '上一页',
	    nextText : '下一页',
	    lastText : '尾页',
	    beforePageText : '第',
	    afterPageText : '页,共{0}页'
	});


	var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
	
	   
   var listGrid = new Ext.grid.GridPanel({
	   id:'csListGrid',
	   title: '采购单信息表',
	   region : 'center',
	   minColumnWidth : 35,
	   loadMask : {msg:'正在加载数据，请稍侯...'}, // 单击分页时显示,为true显示'Loading...'
	   store : csListStore,
	   tbar:[{
		  	text:"采购申请",
			handler:function(){
		   			     if(tmp!=''){
		   						Ext.Ajax.request({
										url : '/weihe/apply_shoppingList',
										success : function(response) {
												var json = Ext.decode(response.responseText);
												if (json.success) {
														Ext.Msg.alert('消息', json.msg, function() {
																	csListStore.reload();
														});
												}else{
														Ext.Msg.alert('消息', json.msg);
												}
										},
						  	   			failure : function() {
												Ext.Msg.alert('错误','网络错误!');
						   				},
										params : 'id=' + tmp
								});
		   							tmp='';
		   					}else{
		   							Ext.Msg.alert('提示','请在下方表中选择需要申请采购的采购单！')
		   					}
					}
	   		},"-",{
			  	text:"进行采购",
				handler:function(){
			   			     if(tmp!=''){
			   						Ext.Ajax.request({
											url : '/weihe/action_shoppingList',
											success : function(response) {
													var json = Ext.decode(response.responseText);
													if (json.success) {
															Ext.Msg.alert('消息', json.msg, function() {
																		csListStore.reload();
															});
													}else{
															Ext.Msg.alert('消息', json.msg);
													}
											},
							  	   			failure : function() {
													Ext.Msg.alert('错误','网络错误!');
							   				},
											params : 'id=' + tmp
									});
			   							tmp='';
			   					}else{
			   							Ext.Msg.alert('提示','请在下方表中选择需要申请采购的采购单！')
			   					}
						}
		   		},"-"],
	   columns: [
	             {header : '采购单号', dataIndex : 'csListNum'},
	             {header : '日期', dataIndex : 'csDate',sortable : true,renderer:Ext.util.Format.dateRenderer('Y年m月d日')},
	             {header : '类型', dataIndex : 'csType', menuDisabled : true},
	             {header : '状态', dataIndex : 'csState', menuDisabled : true},
	             {header : '支付方式', dataIndex : 'csPayment', menuDisabled : true},
	             {header : '总价', dataIndex : 'csTotal', menuDisabled : true},
	             {header : '业务员 ', dataIndex : 'csClerk', menuDisabled : true},
	             {header : '回执', dataIndex : 'csRemark', menuDisabled : true}
	             ],
   sm : rowSelectionModel,
   listeners :{ 
		   'rowclick':function(grid, rowIndex, event) {
		   		var record = grid.getStore().getAt(rowIndex);
		   		tmp = record.id;
		   		Ext.getCmp("csListForm").form.loadRecord(record);
		   		Ext.getCmp("csForm").form.findField('shoppingList.id').setValue(record.id);
		   		Ext.getCmp("csListForm").form.findField('shoppingList.id').setValue(record.id);
		   		Ext.getCmp("csForm").buttons[0].enable();
		   		Ext.getCmp("csForm").buttons[1].enable();
		   		Ext.getCmp("csForm").buttons[2].enable();
		   		Ext.getCmp("csGrid").getStore().baseParams = {'shoppingList.id': record.id};
		   		Ext.getCmp("csGrid").getStore().load({params : {start : 0, limit : 6}});
				Ext.getCmp("csListForm").buttons[0].setText('修改');
	   		}
	   },
	  bbar : pageToolBar
});
	return listGrid;
},
  

shopsForm : function(){
	
		var button_add = new Ext.Button({
			text : '添加',
			disabled : true,
			handler : function() {

					progressConsole();
				if(shopsForm.form.isValid()){ 	
					if (shopsForm.form.findField("sl_Cargo.id").getValue() == "") {
						shopsForm.form.submit({
						  url : '/weihe/addSLCargo_shoppingList',
						   success : function(form, action) {
							   if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								 }else{
								Ext.Msg.alert('消息', action.result.msg, function() {
									 var i = shopsForm.form.findField('shoppingList.id').getValue();
									   shopsForm.form.reset();
									   shopsForm.form.findField('shoppingList.id').setValue(i);
									   Ext.getCmp("csGrid").getStore().baseParams = {'shoppingList.id': i};
									   Ext.getCmp("csGrid").getStore().load({params : {start : 0, limit : 6}});
									   Ext.getCmp("csListGrid").getStore().reload();
								})
							  }
						    },
						   failure : function(form, action) {
								Ext.Msg.alert('错误',action.result.msg);
						   }
					    })
					}else {
						shopsForm.form.submit({
							  url : '/weihe/updateSLCargo_shoppingList',
							   success : function(form, action) {
								   if (action.result.deny) {
									 Ext.Msg.alert('错误', action.result.msg);
								 }else{
									Ext.Msg.alert('消息', action.result.msg, function() {
										var i =  shopsForm.form.findField('shoppingList.id').getValue();
										shopsForm.form.reset();
										shopsForm.form.findField('shoppingList.id').setValue(i);
										Ext.getCmp("csGrid").getStore().load({params : {start : 0, limit : 6}});
										Ext.getCmp("csListGrid").getStore().reload();
										shopsForm.buttons[0].setText('添加');
										
									})
								  }
							    },
						  	   failure : function(form, action) {
									Ext.Msg.alert('错误',action.result.msg);
						   		}
						    })

				
						
					}
			}else{
				Ext.Msg.alert('提示','请输入相关内容');
			}
				}
		
			
		});

	var button_clear = new Ext.Button({
		text : '清空',
		disabled : true,
		handler : function() {
		var i = shopsForm.form.findField('shoppingList.id').getValue();
			shopsForm.form.reset();
			shopsForm.form.findField('shoppingList.id').setValue(i);
			shopsForm.buttons[0].setText('添加');
		}
	});

	var button_del = new Ext.Button({
		text : '删除',
		disabled : true,
		handler : function() {
			var id = shopsForm.form.findField('sl_Cargo.id').getValue();
			if (id == '') {
				Ext.Msg.alert('提示', '请选择需要删除的信息!');
			} else {
				progressConsole();
				Ext.Ajax.request({
					url : '/weihe/deleteSLCargo_shoppingList',
					success : function(response) {
						var json = Ext.decode(response.responseText);
						if (json.success) {
							  if (json.deny) {
									Ext.Msg.alert('错误',json.msg);
									var i = shopsForm.form.findField('shoppingList.id').getValue();
								shopsForm.form.reset();
								shopsForm.form.findField('shoppingList.id').setValue(i);
								shopsForm.buttons[0].setText('添加');
							}else{
							Ext.Msg.alert('消息', json.msg, function() {
								Ext.getCmp("csGrid").getStore().load({params : {start : 0, limit : 6}});
								var i = shopsForm.form.findField('shoppingList.id').getValue();
								shopsForm.form.reset();
								shopsForm.form.findField('shoppingList.id').setValue(i);
								shopsForm.buttons[0].setText('添加');
								Ext.getCmp("csListGrid").getStore().reload();
							});
						  }
						}else{
							Ext.Msg.alert('消息', json.msg);
						}
					},
						 failure : function(form, action) {
									Ext.Msg.alert('错误',action.result.msg);
						   		},
					params: { 'sl_Cargo.id': id , 'shoppingList.id' : shopsForm.form.findField('shoppingList.id').getValue() }
				});
			}
		}
	});
	
		var codestore = new Ext.data.JsonStore({
			url: '/weihe/getCargosByCode_shoppingList',
			root: 'codes',
			fields: ['code']
		
		});
		
		var namestore = new Ext.data.JsonStore({
			url: '/weihe/getCargosByname_shoppingList',
			root: 'names',
			fields: ['name']

		});
		
		var codeCombo = {
				id : 'csCode',
				xtype: 'combo',
				fieldLabel: '产品编号',
				store: codestore,
				typeAhead: false,
				name: 'cargo.code',
				loadingText: '查找中...',
				emptyText: '请输入3位以上编号', 
				minChars: 3,
				pageSize: 0,
				hideTrigger: true,
				editable: true,
				triggerAction: 'all',
				displayField : 'code',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getCode_depotList',
								success : function(response) {
									  var json = Ext.decode(response.responseText);
									  shopsForm.form.findField('csName').setValue(json.name);
									  shopsForm.form.findField('csSpecs').setValue(json.specs);
									  shopsForm.form.findField('csUnit').setValue(json.unit);
									  shopsForm.form.findField('csOrigin').setValue(json.origin);
									  shopsForm.form.findField('csPrice').setValue(json.price);
									  shopsForm.form.findField('cargo.id').setValue(json.id);
									},
								params : {'query' : record.get('code')}
							});
				    }
				}
		};
		var nameCombo = {
				id : 'csName',
				xtype: 'combo',
				fieldLabel: '产品名称',
				store: namestore,
				typeAhead: false,
				name: 'cargo.name',
//				hiddenName : 'cargo.name',
				loadingText: '查找中...',
				emptyText: '请输入产品名称', 
				minChars: 2,
				pageSize: 0,
				hideTrigger: true,
				editable: true,
				triggerAction: 'all',
				displayField : 'name',
				listeners :{ 'select':function(combo, record,index){
							Ext.Ajax.request({
								url : '/weihe/getName_depotList',
								success : function(response) {
									var json = Ext.decode(response.responseText);
									  shopsForm.form.findField('csName').setValue(json.name);
									  shopsForm.form.findField('csSpecs').setValue(json.specs);
									  shopsForm.form.findField('csOrigin').setValue(json.origin);
									  shopsForm.form.findField('csUnit').setValue(json.unit);
									  shopsForm.form.findField('csPrice').setValue(json.price);
									  shopsForm.form.findField('cargo.id').setValue(json.id);
									},
									params : {'query' : record.get('name')}
							});
				    }
				}
		}
		

	var shopsForm = new Ext.form.FormPanel({
		id : 'csForm',
		title : '采购货物信息',
		frame : true,
		width : 300,
		region : 'east',
		autoScroll : true,
		labelAlign : 'right',
		labelWidth : 60,
		defaultType : 'textfield',
			defaults : {
			width : 200
		},
		items : [{  // Ext.form.Hidden
			id : 'csId',
			xtype : 'hidden',  // 隐藏的表单
			name : 'sl_Cargo.id'
		},{  // Ext.form.Hidden
			xtype : 'hidden',  // 隐藏的表单
			name : 'shoppingList.id'
		},{  // Ext.form.Hidden
			xtype : 'hidden',  // 隐藏的表单
			name : 'cargo.id'
		},codeCombo,nameCombo,{
			id:'csSpecs',
			fieldLabel : '规格型号',
			name : 'cargo.specs'
		},{
			id:'csOrigin',
			fieldLabel : '产地',
			name : 'cargo.origin'
		},{
			id:'csUnit',
			fieldLabel : '单位',
			name : 'cargo.unit'
		},{
			id:'csPrice',
			fieldLabel : '单价',
			allowBlank:false,
			blankText:'请输入单价',
			regex : /^\d+(\.\d+)?$/,
			regexText:'请输入有效的价格', 
			name : 'sl_Cargo.price'
		},{
			id:'csAmount',
			fieldLabel : '数量',
			allowBlank:false,
			name : 'sl_Cargo.amount',
			regex : /^[1-9]\d*$/,
			regexText:'请输入有效的数量', 
			blankText:'请输入数量'
		},{
			id:'csRemark',
			fieldLabel : '备注',
			name : 'sl_Cargo.remark'
		}],
		buttons : [button_add,button_clear,button_del]
	
});
	return shopsForm;
},


	
grid : function(){
	 


	var pageToolBar = new Ext.PagingToolbar({
		pageSize : 6,
		store : csStore,
		displayInfo : true,
		displayMsg : '当前显示{0}-{1}条记录,共{2}条记录',
	    emptyMsg : "没有记录显示!",
	    firstText : '首页',
	    prevText : '上一页',
	    nextText : '下一页',
	    lastText : '尾页',
	    beforePageText : '第',
	    afterPageText : '页,共{0}页'
	   


	});


	var rowSelectionModel = new Ext.grid.RowSelectionModel({singleSelect : true});
	
	   
   var grid = new Ext.grid.GridPanel({
	   id:'csGrid',
	   title : '采购单货物表',
	   border:'true',
	   region : 'center',
	   width: 450,
	   minColumnWidth : 50,
	   loadMask : {msg:'正在加载数据，请稍侯...'}, // 单击分页时显示,为true显示'Loading...'
	   store : csStore,
	   columns: [
	             {header : '产品编号', dataIndex : 'csCode', menuDisabled : true},
	             {header : '产品名称', dataIndex : 'csName'},
	             {header : '规格型号', dataIndex : 'csSpecs', menuDisabled : true},
	             {header : '单位', dataIndex : 'csUnit', menuDisabled : true},
	             {header : '单价', dataIndex : 'csPrice',sortable : true},
	             {header : '数量', dataIndex : 'csAmount', menuDisabled : true},
	             {header : '金额', dataIndex : 'csSum', menuDisabled : true},
	             {header : '备注', dataIndex : 'csRemark', menuDisabled : true}
    ],
       sm : rowSelectionModel,
       listeners :{ 
		   'rowclick':function(grid, rowIndex, event) {
		   var record = grid.getStore().getAt(rowIndex);
		   Ext.getCmp("csForm").form.loadRecord(record);
		   Ext.getCmp("csForm").buttons[0].enable();
		   Ext.getCmp("csForm").buttons[1].enable();
		   Ext.getCmp("csForm").buttons[2].enable();
		   Ext.getCmp("csForm").buttons[0].setText('修改');
		  
	   }
	},
	  viewConfig : {
	     forceFit : false
	  },
	  bbar : pageToolBar
});
	return grid;
	}
  }
}();








